import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.*;

import java.io.IOException;


public class MySqlBinlogSample {

    public static void main(String[] args) {
        String url = "127.0.0.1", username = "test", password = "123456";
        for (String arg : args) {
//            System.out.println("arg: " + arg);
            if (arg.startsWith("database.url=")) {
                url = arg.substring("database.url=".length());
            } else if (arg.startsWith("database.username=")) {
                username = arg.substring("database.username=".length());
            } else if (arg.startsWith("database.password=")) {
                password = arg.substring("database.password=".length());
            }
        }
        BinaryLogClient client = new BinaryLogClient(url, 3306, username, password);
        client.setServerId(2);

        client.registerEventListener(event -> {
            System.out.println("==============");
            EventData data = event.getData();
            if (data instanceof TableMapEventData) {
                System.out.println("Table:");
                TableMapEventData tableMapEventData = (TableMapEventData) data;
                System.out.println(tableMapEventData.getTableId() + ": [" + tableMapEventData.getDatabase() + "].[" + tableMapEventData.getTable() + "]");
            }
            if (data instanceof UpdateRowsEventData) {
                System.out.println("Update:");
                System.out.println(data);
            } else if (data instanceof WriteRowsEventData) {
                System.out.println("Insert:");
                System.out.println(data);
            } else if (data instanceof DeleteRowsEventData) {
                System.out.println("Delete:");
                System.out.println(data);
            }
        });

        try {
            client.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
